﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using SAC.Helper;
using SAC.Power;
using SAC.DAL.PublicMethod;
using SAC.sqlHelper;
using SAC.DB2;
using System.Data;

namespace SAC.DAL.HourRep
{
    public class DALHoruRep
    {
        int count;
        int countKey;
        string sql = "";
        string errMsg = "";
        string TSMS = "";       //调试模式
        string rtDBType = "";   //实时数据库
        string rlDBType = "";   //关系数据库

        SAC.Elink.Elink ek = new SAC.Elink.Elink();
        SAC.Plink.Plink pk = new SAC.Plink.Plink();
        StrHelper sh = new StrHelper();
        PowerCale pc = new PowerCale();
        DALPubMehtod pm = new DALPubMehtod();
        DALUtilityMehtod um = new DALUtilityMehtod();
        LogHelper lh = new LogHelper();

        /// <summary>
        /// 初始化函数
        /// 初始化报表配置
        /// 对全局变量赋值
        /// </summary>
        /// <returns></returns>
        private void init()
        {
            TSMS = IniHelper.ReadIniData("Report", "TSMS", null);
            rtDBType = IniHelper.ReadIniData("RTDB", "DBType", null);
            rlDBType = IniHelper.ReadIniData("RelationDBbase", "DBType", null);
        }


        /// <summary>
        /// 小时报表
        /// </summary>
        /// <param name="repName">报表名称</param>
        /// <param name="unit">机组</param>
        /// <param name="date">查询日期</param>
        /// <param name="isDay">是否是当前日期</param>
        /// <returns></returns>
        public ArrayList RetArrsRepDayInfo(string repName, int unit, DateTime date, int isDay)
        {
            this.init();

            int days;
            object obj;
            string bm = "";
            string mon = "";
            string res = "";
            string num = "";
            string str = "";
            string sDate = "";
            string GYID = "";   //公用库参数ID
            string ParaID = ""; //参数ID 
            string fDay = "";
            string lDay = "";
            string dlID = "";
            string pName = "";
            string dType = "小时点";
            string mType = "日点";
            string repType = "日明细";
            int id;
            int ret = 0;
            double drvA = 0;
            double drvB = 0;
            double valGY = 0;

            string[] zb = null;
            string[] un = null;
            string[] lay = null;
            string[] dv = null;
            string[] dvExcel = null;
            string[] qsrq = null;
            string[] jsrq = null;

            DataTable dtGY = null;

            Hashtable htGY = new Hashtable();
            Hashtable htUn = new Hashtable();
            ArrayList list = new ArrayList();

            //参数个数
            count = pm.GetCount(repName);
            days = DateTime.DaysInMonth(date.Year, date.Month);

            #region 日期

            int hours = 0;
            if (isDay == 0)
                hours = Convert.ToInt32(date.Hour.ToString());
            else
                //hours = 24;
                hours = 25;
            if (repName == "报表十七" || repName == "报表十八")
            {
                qsrq = new string[hours / 8 + 1];
                jsrq = new string[hours / 8 + 1];
            }
            else if (repName == "峰谷腰报表")
            {
                qsrq = new string[6];
                jsrq = new string[6];
                hours = 6;
            }
            else if (repName == "技术经济指标日报表")
            {
                qsrq = new string[2];
                jsrq = new string[2];
                hours = 2;
            }
            else
            {
                qsrq = new string[hours];
                jsrq = new string[hours];
            }

            for (int i = 0; i <= hours; i++)
            {

                if (repName == "报表十七" || repName == "报表十八")
                {
                    if (i == 0)
                    {
                        qsrq[i] = date.Year + "-" + date.Month + "-" + date.AddDays(-1).Day + " 16:00:00";
                        jsrq[i] = date.Year + "-" + date.Month + "-" + date.AddDays(-1).Day + " 23:59:59";
                    }
                    else if (i % 8 == 0)
                    {
                        if (i / 8 == 0)
                        {
                            qsrq[i / 8] = date.Year + "-" + date.Month + "-" + date.Day + " 0:00:00";
                        }
                        else
                        {
                            qsrq[i / 8] = date.Year + "-" + date.Month + "-" + date.Day + " " + ((i / 8 - 1) * 8) + ":00:00";
                        }
                        jsrq[i / 8] = date.Year + "-" + date.Month + "-" + date.Day + " " + ((i / 8) * 8 - 1) + ":59:59";
                    }
                    else if (hours == 23)
                    {
                        jsrq[(i / 8) + 1] = date.Year + "-" + date.Month + "-" + date.Day + " " + i + ":59:59";
                    }
                }
                else if (repName == "峰谷腰报表")
                {
                    qsrq[0] = date.Year + "-" + date.Month + "-" + date.AddDays(-1).Day + " 23:00:00";
                    jsrq[0] = date.Year + "-" + date.Month + "-" + date.AddDays(-1).Day + " 23:59:59";
                    qsrq[1] = date.Year + "-" + date.Month + "-" + date.Day + " 0:00:00";
                    jsrq[1] = date.Year + "-" + date.Month + "-" + date.Day + " 7:59:59";
                    qsrq[2] = date.Year + "-" + date.Month + "-" + date.Day + " 8:00:00";
                    jsrq[2] = date.Year + "-" + date.Month + "-" + date.Day + " 11:59:59";
                    qsrq[3] = date.Year + "-" + date.Month + "-" + date.Day + " 12:00:00";
                    jsrq[3] = date.Year + "-" + date.Month + "-" + date.Day + " 16:59:59";
                    qsrq[4] = date.Year + "-" + date.Month + "-" + date.Day + " 17:00:00";
                    jsrq[4] = date.Year + "-" + date.Month + "-" + date.Day + " 20:59:59";
                    qsrq[5] = date.Year + "-" + date.Month + "-" + date.Day + " 21:00:00";
                    jsrq[5] = date.Year + "-" + date.Month + "-" + date.Day + " 23:59:59";
                }
                else if (repName == "技术经济指标日报表")
                {
                    if (i < 2)
                    {
                        qsrq[i] = date.Year + "-" + date.Month + "-" + date.AddDays(-i).Day + " :00:00";
                        jsrq[i] = date.Year + "-" + date.Month + "-" + date.AddDays(-i).Day + " 23:59:59";
                    }
                }
                else
                {
                    #region 湖南长沙修改 0-24
                    if (i != 25)
                    {

                        if (i < hours)
                        {
                            if (i == 0)
                            {
                                qsrq[0] = date.Year + "-" + date.Month + "-" + (date.Day - 1) + " 23:00:00";
                                jsrq[0] = date.Year + "-" + date.Month + "-" + (date.Day - 1) + " 23:59:59";
                            }
                            else
                            {
                                qsrq[i] = date.Year + "-" + date.Month + "-" + date.Day + " " + (i - 1) + ":00:00";
                                jsrq[i] = date.Year + "-" + date.Month + "-" + date.Day + " " + (i - 1) + ":59:59";
                            }

                        }
                    }
                    #endregion
                    //if (i < hours)
                    //{
                    //    qsrq[i] = date.Year + "-" + date.Month + "-" + date.Day + " " + i + ":00:00";
                    //    jsrq[i] = date.Year + "-" + date.Month + "-" + date.Day + " " + i + ":59:59";
                    //}
                }
            }

            fDay = qsrq[0];

            if (hours == 24)
                lDay = DateTime.Now.ToString();
            else
                lDay = jsrq[jsrq.Length - 1];
            #endregion

            zb = new string[count + 1];
            un = new string[count + 1];
            lay = new string[count + 1];
            dv = new string[count + 1];
            dvExcel = new string[count + 1];

            #region 公用库读取

            sql = "select * from GYReport where 报表名称='全厂级'";

            if (rlDBType == "SQL")
                dtGY = DBsql.RunDataTable(sql, out errMsg);
            else
                dtGY = DBdb2.RunDataTable(sql, out errMsg);

            if (dtGY != null && dtGY.Rows.Count > 0)
            {
                LogHelper.WriteLog(LogHelper.EnLogType.Run, DateTime.Now.ToString("yyyy-MM-dd H:mm:ss") + "---" + repName + "----" + repType + "----公用库轮转开始!");

                for (int k = 1; k <= hours; k++)
                {
                    if (k < qsrq.Length)
                    {
                        if (qsrq[k] != null)
                            sDate = DateTime.Parse(qsrq[k]).ToString("yyyy-MM-dd 23:59:59");

                        for (int i = 0; i < dtGY.Rows.Count; i++)
                        {
                            GYID = dtGY.Rows[i]["参数ID"].ToString();

                            if (dtGY.Rows[i][9].ToString() == "平均" || dtGY.Rows[i][9].ToString() == "累计")
                            {
                                pName = dtGY.Rows[i][dType].ToString();

                                if (qsrq[k] != null)
                                {
                                    if (rtDBType == "PI")
                                        pk.GetHisValue(pName, sDate, ref valGY);
                                    else if (rtDBType == "EDNA")
                                        ek.GetHisValueByTime(pName, sDate, ref ret, ref valGY);
                                }
                                else
                                    valGY = 0;

                                if (GYID != "")
                                {
                                    if (htGY.Contains(GYID))
                                    {
                                        str = htGY[GYID] + valGY.ToString() + ",";
                                        htGY.Remove(GYID);
                                        htGY.Add(GYID, str);
                                    }
                                    else
                                    {
                                        str = valGY + ",";
                                        htGY.Add(GYID, str);
                                    }
                                }
                            }
                            else if (dtGY.Rows[i][9].ToString() == "电量")
                            {
                                dlID = dtGY.Rows[i]["电量点"].ToString();

                                if (qsrq[k] != null)
                                    valGY = double.Parse(pc.GetPower(valGY, dlID, qsrq[k], jsrq[k], bm).ToString());
                                else
                                    valGY = 0;

                                mon = "";

                                if (dtGY.Rows[i]["公式"].ToString() != "")
                                    mon = valGY.ToString() + dtGY.Rows[i]["公式"].ToString();
                                else
                                    mon = valGY.ToString();

                                res = "";
                                try
                                { res = StrHelper.Cale(mon); }
                                catch
                                { res = "0"; }

                                if (GYID != "")
                                {
                                    if (htGY.Contains(GYID))
                                    {
                                        str = htGY[GYID] + res + ",";
                                        htGY.Remove(GYID);
                                        htGY.Add(GYID, str);
                                    }
                                    else
                                    {
                                        str = res + ",";
                                        htGY.Add(GYID, str);
                                    }
                                }
                            }
                            else if (dtGY.Rows[i][9].ToString() == "SQL")
                            {
                                sql = dtGY.Rows[i]["SQL语句"].ToString();

                                if (qsrq[k] != null)
                                {
                                    if (sql.Contains("&bt&") && sql.Contains("&et&"))
                                    {
                                        sql = sql.Replace("&bt&", qsrq[k].ToString());
                                        sql = sql.Replace("&et&", sDate);
                                    }

                                    if (rlDBType == "SQL")
                                        obj = DBsql.RunSingle(sql, out errMsg);
                                    else
                                        obj = DBdb2.RunSingle(sql, out errMsg);

                                    res = "";
                                    if (obj != null && obj != "")
                                    {
                                        if (obj.ToString() != "")
                                            res = obj.ToString();
                                        else
                                            res = "0";
                                    }
                                    else
                                    { res = "0"; }
                                }
                                else
                                    res = "0";

                                if (GYID != "")
                                {
                                    if (htGY.Contains(GYID))
                                    {
                                        str = htGY[GYID] + res + ",";
                                        htGY.Remove(GYID);
                                        htGY.Add(GYID, str);
                                    }
                                    else
                                    {
                                        str = res + ",";
                                        htGY.Add(GYID, str);
                                    }
                                }
                            }
                        }
                    }
                }

                //公用库月值
                sDate = DateTime.Parse(lDay).AddSeconds(-1).ToString("yyyy-MM-dd 23:59:59");

                for (int i = 0; i < dtGY.Rows.Count; i++)
                {
                    GYID = dtGY.Rows[i]["参数ID"].ToString();

                    if (dtGY.Rows[i][9].ToString() == "平均" || dtGY.Rows[i][9].ToString() == "累计")
                    {
                        pName = dtGY.Rows[i][mType].ToString();

                        if (rtDBType == "PI")
                            pk.GetHisValue(pName, sDate, ref valGY);
                        else if (rtDBType == "EDNA")
                            ek.GetHisValueByTime(pName, sDate, ref ret, ref valGY);

                        if (GYID != "")
                        {
                            if (htGY.Contains(GYID))
                            {
                                str = htGY[GYID] + valGY.ToString() + ",";
                                htGY.Remove(GYID);
                                htGY.Add(GYID, str);
                            }
                            else
                            {
                                str = valGY + ",";
                                htGY.Add(GYID, str);
                            }
                        }
                    }
                    else if (dtGY.Rows[i][9].ToString() == "电量")
                    {
                        dlID = dtGY.Rows[i]["电量点"].ToString();

                        valGY = double.Parse(pc.GetPower(valGY, dlID, fDay, lDay, bm).ToString());

                        mon = "";

                        if (dtGY.Rows[i]["公式"].ToString() != "")
                            mon = valGY.ToString() + dtGY.Rows[i]["公式"].ToString();
                        else
                            mon = valGY.ToString();

                        res = "";
                        try
                        { res = StrHelper.Cale(mon); }
                        catch
                        { res = "0"; }

                        if (GYID != "")
                        {
                            if (htGY.Contains(GYID))
                            {
                                str = htGY[GYID] + res + ",";
                                htGY.Remove(GYID);
                                htGY.Add(GYID, str);
                            }
                            else
                            {
                                str = res + ",";
                                htGY.Add(GYID, str);
                            }
                        }
                    }
                    else if (dtGY.Rows[i][9].ToString() == "SQL")
                    {
                        sql = dtGY.Rows[i]["SQL语句"].ToString();

                        if (sql.Contains("&bt&") && sql.Contains("&et&"))
                        {
                            sql = sql.Replace("&bt&", fDay);
                            sql = sql.Replace("&et&", sDate);
                        }

                        if (rlDBType == "SQL")
                            obj = DBsql.RunSingle(sql, out errMsg);
                        else
                            obj = DBdb2.RunSingle(sql, out errMsg);

                        res = "";
                        if (obj != null && obj != "")
                        {
                            if (obj.ToString() != "")
                                res = obj.ToString();
                            else
                                res = "0";
                        }
                        else
                        { res = "0"; }

                        if (GYID != "")
                        {
                            if (htGY.Contains(GYID))
                            {
                                str = htGY[GYID] + res + ",";
                                htGY.Remove(GYID);
                                htGY.Add(GYID, str);
                            }
                            else
                            {
                                str = res + ",";
                                htGY.Add(GYID, str);
                            }
                        }
                    }
                }

                LogHelper.WriteLog(LogHelper.EnLogType.Run, DateTime.Now.ToString("yyyy-MM-dd H:mm:ss") + "---" + repName + "----" + repType + "----公用库轮转结束!");
            }
            #endregion

            #region 机组数据读取

            LogHelper.WriteLog(LogHelper.EnLogType.Run, DateTime.Now.ToString("yyyy-MM-dd H:mm:ss") + "---" + repName + "----" + repType + "----机组轮转开始!");

            for (int k = 0; k < hours; k++)
            {
                if (k < qsrq.Length)
                {
                    if (qsrq[k] != null)
                        sDate = jsrq[k];

                    sql = "SELECT DISTINCT 公式级别 FROM T_sheet_sheetPara where 报表名称='" + repName + "' and 机组=" + unit + " and 公式级别!=0";

                    DataTable dtLel = null;

                    if (rlDBType == "SQL")
                        dtLel = DBsql.RunDataTable(sql, out errMsg);
                    else
                        dtLel = DBdb2.RunDataTable(sql, out errMsg);

                    if (dtLel.Rows.Count > 0)
                    {
                        for (int j = 0; j < dtLel.Rows.Count; j++)
                        {
                            string gsjb = dtLel.Rows[j]["公式级别"].ToString();

                            #region 平均
                            sql = "select * from T_sheet_sheetPara where 报表名称='" + repName + "' and 机组=" + unit + " and 参数类型='平均' and 公式级别=" + gsjb;

                            DataTable dtAvg = null;

                            if (rlDBType == "SQL")
                                dtAvg = DBsql.RunDataTable(sql, out errMsg);
                            else
                                dtAvg = DBdb2.RunDataTable(sql, out errMsg);

                            if (dtAvg != null && dtAvg.Rows.Count > 0)
                            {
                                for (int i = 0; i < dtAvg.Rows.Count; i++)
                                {
                                    num = dtAvg.Rows[i]["小数点数"].ToString();

                                    pName = dtAvg.Rows[i][dType].ToString();
                                    //参数ID
                                    ParaID = dtAvg.Rows[i]["参数ID"].ToString();
                                    //参数顺序
                                    id = int.Parse(dtAvg.Rows[i]["参数顺序"].ToString());

                                    if (qsrq[k] != null)
                                    {
                                        if (rtDBType == "EDNA")
                                            ek.GetHisValueByTime(pName, sDate, ref ret, ref drvA);
                                        else
                                            if (dtAvg.Rows[i]["参数名"].ToString() == "供热量1" || dtAvg.Rows[i]["参数名"].ToString() == "耗用天然气1" || dtAvg.Rows[i]["参数名"].ToString() == "供热量2" || dtAvg.Rows[i]["参数名"].ToString() == "耗用天然气2" || dtAvg.Rows[i]["参数名"].ToString() == "供热量3" || dtAvg.Rows[i]["参数名"].ToString() == "耗用天然气3")
                                            {
                                                pk.GetAVGValuestring(pName, Convert.ToDateTime(jsrq[0]).ToString("yyyy-MM-dd 0:00:00").ToString(), jsrq[0], ref drvA);
                                            }
                                            else
                                            {
                                                //LogHelper.WriteLog(LogHelper.EnLogType.Run, "开始时间:" + DateTime.Now.ToString("yyyy-MM-dd H:mm:ss") + "--" + i + "-" + repName + "----" + repType + "----机组开始!");
                                                pk.GetHisValue(pName, sDate, ref drvA);
                                                //LogHelper.WriteLog(LogHelper.EnLogType.Run, "结束时间:" + DateTime.Now.ToString("yyyy-MM-dd H:mm:ss") + "---" + repName + "----" + repType + "----机组结束!");
                                            }
                                    }
                                    else
                                        drvA = 0;

                                    if (id != 0)
                                    {
                                        zb[id] = dtAvg.Rows[i]["参数名"].ToString();
                                        un[id] = dtAvg.Rows[i]["单位"].ToString();
                                        lay[id] = dtAvg.Rows[i]["设计值"].ToString();

                                        dv[id] += sh.ShowPoint(drvA.ToString(), num) + ",";
                                        dvExcel[id] += drvA.ToString() + ",";
                                    }

                                    if (ParaID != "")
                                    {
                                        if (htUn.Contains(ParaID))
                                        {
                                            str = htUn[ParaID] + drvA.ToString() + ",";
                                            htUn.Remove(ParaID);
                                            htUn.Add(ParaID, str);
                                        }
                                        else
                                        {
                                            str = drvA.ToString() + ",";
                                            htUn.Add(ParaID, str);
                                        }
                                    }
                                }
                            }
                            #endregion

                            #region 累计
                            sql = "select * from T_sheet_sheetPara where 报表名称='" + repName + "' and 机组=" + unit + " and 参数类型='累计' and 公式级别=" + gsjb;

                            DataTable dtDiff = null;

                            if (rlDBType == "SQL")
                                dtDiff = DBsql.RunDataTable(sql, out errMsg);
                            else
                                dtDiff = DBdb2.RunDataTable(sql, out errMsg);

                            if (dtDiff != null && dtDiff.Rows.Count > 0)
                            {
                                for (int i = 0; i < dtDiff.Rows.Count; i++)
                                {
                                    num = dtDiff.Rows[i]["小数点数"].ToString();

                                    pName = dtDiff.Rows[i][dType].ToString();
                                    //参数ID
                                    ParaID = dtDiff.Rows[i]["参数ID"].ToString();
                                    //参数顺序
                                    id = int.Parse(dtDiff.Rows[i]["参数顺序"].ToString());

                                    drvA = 0;

                                    if (qsrq[k] != null)
                                    {
                                        if (rtDBType == "EDNA")
                                            ek.GetHisValueByTime(pName, sDate, ref ret, ref drvA);
                                        else
                                            pk.GetHisValue(pName, sDate, ref drvA);
                                    }
                                    else
                                        drvA = 0;

                                    if (id != 0)
                                    {
                                        zb[id] = dtDiff.Rows[i]["参数名"].ToString();
                                        un[id] = dtDiff.Rows[i]["单位"].ToString();
                                        lay[id] = dtDiff.Rows[i]["设计值"].ToString();

                                        dv[id] += sh.ShowPoint(drvA.ToString(), num) + ",";
                                        dvExcel[id] += drvA.ToString() + ",";
                                    }

                                    if (ParaID != "")
                                    {
                                        if (htUn.Contains(ParaID))
                                        {
                                            str = htUn[ParaID] + drvA.ToString() + ",";
                                            htUn.Remove(ParaID);
                                            htUn.Add(ParaID, str);
                                        }
                                        else
                                        {
                                            str = drvA.ToString() + ",";
                                            htUn.Add(ParaID, str);
                                        }
                                    }
                                }
                            }

                            #endregion

                            #region SQL

                            sql = "select * from T_sheet_sheetPara where 报表名称='" + repName + "' and 机组=" + unit + " and 参数类型='SQL' and 公式级别=" + gsjb;

                            DataTable dtSQL = null;

                            if (rlDBType == "SQL")
                                dtSQL = DBsql.RunDataTable(sql, out errMsg);
                            else
                                dtSQL = DBdb2.RunDataTable(sql, out errMsg);

                            if (dtSQL != null && dtSQL.Rows.Count > 0)
                            {
                                for (int i = 0; i < dtSQL.Rows.Count; i++)
                                {
                                    num = dtSQL.Rows[i]["小数点数"].ToString();

                                    sql = dtSQL.Rows[i]["SQL语句"].ToString();
                                    //参数ID
                                    ParaID = dtSQL.Rows[i]["参数ID"].ToString();
                                    //参数顺序
                                    id = int.Parse(dtSQL.Rows[i]["参数顺序"].ToString());

                                    if (qsrq[k] != null)
                                    {
                                        if (sql.Contains("&bt&") && sql.Contains("&et&"))
                                        {
                                            sql = sql.Replace("&bt&", qsrq[k].ToString());
                                            sql = sql.Replace("&et&", sDate);
                                        }

                                        if (rlDBType == "SQL")
                                            obj = DBsql.RunSingle(sql, out errMsg);
                                        else
                                            obj = DBdb2.RunSingle(sql, out errMsg);

                                        res = "";
                                        if (obj != null && obj != "")
                                        {
                                            if (obj.ToString() != "")
                                                res = obj.ToString();
                                            else
                                                res = "0";
                                        }
                                        else
                                        { res = "0"; }
                                    }
                                    else
                                        res = "0";

                                    if (id != 0)
                                    {
                                        zb[id] = dtSQL.Rows[i]["参数名"].ToString();
                                        un[id] = dtSQL.Rows[i]["单位"].ToString();
                                        lay[id] = dtSQL.Rows[i]["设计值"].ToString();

                                        dv[id] += sh.ShowPoint(res, num) + ",";
                                        dvExcel[id] += sh.ShowPoint(res, "-1") + ",";
                                    }

                                    if (ParaID != "")
                                    {
                                        if (htUn.Contains(ParaID))
                                        {
                                            str = htUn[ParaID] + res + ",";
                                            htUn.Remove(ParaID);
                                            htUn.Add(ParaID, str);
                                        }
                                        else
                                        {
                                            str = res + ",";
                                            htUn.Add(ParaID, str);
                                        }
                                    }
                                }
                            }
                            #endregion

                            #region 电量
                            sql = "select * from T_sheet_sheetPara where 报表名称='" + repName + "' and 机组=" + unit + " and 参数类型='电量' and 公式级别=" + gsjb;

                            DataTable dtDL = null;

                            if (rlDBType == "SQL")
                                dtDL = DBsql.RunDataTable(sql, out errMsg);
                            else
                                dtDL = DBdb2.RunDataTable(sql, out errMsg);

                            if (dtDL.Rows.Count > 0 && dtDL != null)
                            {
                                for (int i = 0; i < dtDL.Rows.Count; i++)
                                {
                                    num = dtDL.Rows[i]["小数点数"].ToString();

                                    dlID = dtDL.Rows[i]["电量点"].ToString();
                                    //参数ID
                                    ParaID = dtDL.Rows[i]["参数ID"].ToString();
                                    //参数顺序
                                    id = int.Parse(dtDL.Rows[i]["参数顺序"].ToString());

                                    if (qsrq[k] != null)
                                        drvA = double.Parse(pc.GetPower(drvA, dlID, qsrq[k], jsrq[k], bm).ToString());
                                    else
                                        drvA = 0;

                                    mon = "";

                                    if (dtDL.Rows[i]["公式"].ToString() != "")
                                        mon = drvA.ToString() + dtDL.Rows[i]["公式"].ToString();
                                    else
                                        mon = drvA.ToString();

                                    res = "";
                                    try
                                    { res = StrHelper.Cale(mon); }
                                    catch
                                    { res = "0"; }

                                    if (id != 0)
                                    {
                                        zb[id] = dtDL.Rows[i]["参数名"].ToString();
                                        un[id] = dtDL.Rows[i]["单位"].ToString();
                                        lay[id] = dtDL.Rows[i]["设计值"].ToString();

                                        dv[id] += sh.ShowPoint(res, num) + ",";
                                        dvExcel[id] += sh.ShowPoint(res, "-1") + ",";
                                    }

                                    if (ParaID != "")
                                    {
                                        if (htUn.Contains(ParaID))
                                        {
                                            str = htUn[ParaID] + res + ",";
                                            htUn.Remove(ParaID);
                                            htUn.Add(ParaID, str);
                                        }
                                        else
                                        {
                                            str = res + ",";
                                            htUn.Add(ParaID, str);
                                        }
                                    }
                                }
                            }

                            #endregion

                            #region 其它
                            sql = "select * from T_sheet_sheetPara where 报表名称='" + repName + "' and 机组=" + unit + " and 参数类型='其它' and 公式级别=" + gsjb;

                            DataTable dtQT = null;

                            if (rlDBType == "SQL")
                                dtQT = DBsql.RunDataTable(sql, out errMsg);
                            else
                                dtQT = DBdb2.RunDataTable(sql, out errMsg);

                            if (dtQT.Rows.Count > 0 && dtQT != null)
                            {
                                for (int i = 0; i < dtQT.Rows.Count; i++)
                                {
                                    num = dtQT.Rows[i]["小数点数"].ToString();
                                    //参数ID
                                    ParaID = dtQT.Rows[i]["参数ID"].ToString();
                                    //参数顺序
                                    id = int.Parse(dtQT.Rows[i]["参数顺序"].ToString());

                                    #region 购网电量
                                    if (dtQT.Rows[i]["参数名"].ToString() == "购网电量")
                                    {
                                        //string re = "0";

                                        //if (qsrq[k] != null)
                                        //    re = um.gwdl(qsrq[k].ToString(), jsrq[k].ToString(), unit);

                                        //if (dtQT.Rows[i]["显示"].ToString() == "1")
                                        //{
                                        //    id = int.Parse(dtQT.Rows[i]["参数顺序"].ToString());

                                        //    dv[id] += sh.ShowPoint(re.ToString(), num) + ",";

                                        //    dvExcel[id] += sh.ShowPoint(re.ToString(), "-1") + ",";
                                        //}

                                        //if (dtQT.Rows[i]["参数ID"].ToString() != "")
                                        //{
                                        //    id = int.Parse(dtQT.Rows[i]["参数key"].ToString());

                                        //    du[id] = dtQT.Rows[i]["参数ID"].ToString() + "," + re.ToString() + ";";
                                        //}
                                    }
                                    #endregion

                                    #region 入炉煤低位发热量
                                    if (dtQT.Rows[i]["参数名"].ToString() == "入炉煤低位发热量")
                                    {
                                        //string rlm = um.rlmdwfrl("日报", qsrq[k], jsrq[k], 1);

                                        //if (dtQT.Rows[i]["显示"].ToString() == "1")
                                        //{
                                        //    id = int.Parse(dtQT.Rows[i]["参数顺序"].ToString());

                                        //    dv[id] += sh.ShowPoint(rlm, num) + ",";

                                        //    dvExcel[id] += sh.ShowPoint(rlm, "-1") + ",";
                                        //}

                                        //if (dtQT.Rows[i]["参数ID"].ToString() != "")
                                        //{
                                        //    id = int.Parse(dtQT.Rows[i]["参数key"].ToString());

                                        //    //if (num == "")
                                        //    du[id] = dtQT.Rows[i]["参数ID"].ToString() + "," + rlm + ";";
                                        //    //else
                                        //    //    du[id] = dtQT.Rows[i]["参数ID"].ToString() + "," + sh.ShowPoint(rlm, num) + ";";
                                        //}
                                    }
                                    #endregion
                                }
                            }
                            #endregion

                            #region 公式
                            sql = "select * from T_sheet_sheetPara where 报表名称='" + repName + "' and 机组=" + unit + " and 参数类型='公式' and 公式级别=" + gsjb;

                            DataTable dtMon = null;

                            if (rlDBType == "SQL")
                                dtMon = DBsql.RunDataTable(sql, out errMsg);
                            else
                                dtMon = DBdb2.RunDataTable(sql, out errMsg);

                            if (dtMon != null && dtMon.Rows.Count > 0)
                            {
                                for (int i = 0; i < dtMon.Rows.Count; i++)
                                {
                                    num = dtMon.Rows[i]["小数点数"].ToString();
                                    //参数ID
                                    ParaID = dtMon.Rows[i]["参数ID"].ToString();
                                    //参数顺序
                                    id = int.Parse(dtMon.Rows[i]["参数顺序"].ToString());

                                    string pMon = dtMon.Rows[i]["公式"].ToString();

                                    string paras = dtMon.Rows[i]["公式参数"].ToString();

                                    string month = pm.retMonByMonthAndYear(htGY, htUn, paras, pMon);

                                    if (month != "")
                                    {
                                        res = "";
                                        try
                                        { res = StrHelper.Cale(month); }
                                        catch
                                        { res = "0"; }

                                        if (id != 0)
                                        {
                                            zb[id] = dtMon.Rows[i]["参数名"].ToString();
                                            un[id] = dtMon.Rows[i]["单位"].ToString();
                                            lay[id] = dtMon.Rows[i]["设计值"].ToString();

                                            if (qsrq[k] != null)
                                                dv[id] += sh.ShowPoint(res, num) + ",";
                                            else
                                                dv[id] += sh.ShowPoint("0", num) + ",";

                                            if (qsrq[k] != null)
                                                dvExcel[id] += sh.ShowPoint(res, "-1") + ",";
                                            else
                                                dvExcel[id] += "0" + ",";
                                        }

                                        if (ParaID != "")
                                        {
                                            if (htUn.Contains(ParaID))
                                            {
                                                str = htUn[ParaID] + res + ",";
                                                htUn.Remove(ParaID);
                                                htUn.Add(ParaID, str);
                                            }
                                            else
                                            {
                                                str = res + ",";
                                                htUn.Add(ParaID, str);
                                            }
                                        }
                                    }
                                }
                            }
                            #endregion
                        }
                    }
                }
            }

            LogHelper.WriteLog(LogHelper.EnLogType.Run, DateTime.Now.ToString("yyyy-MM-dd H:mm:ss") + "---" + repName + "----" + repType + "----机组轮转结束!");

            #endregion

            #region 全厂数据读取
            sql = "SELECT DISTINCT 公式级别 FROM T_sheet_sheetPara where 报表名称='" + repName + "' and 机组=0 and 公式级别!=0";

            DataTable dtLelMonth = null;

            if (rlDBType == "SQL")
                dtLelMonth = DBsql.RunDataTable(sql, out errMsg);
            else
                dtLelMonth = DBdb2.RunDataTable(sql, out errMsg);

            if (dtLelMonth.Rows.Count > 0)
            {
                LogHelper.WriteLog(LogHelper.EnLogType.Run, DateTime.Now.ToString("yyyy-MM-dd H:mm:ss") + "---" + repName + "----" + repType + "----全厂开始!");

                if (isDay == 0)
                    sDate = DateTime.Parse(lDay).AddSeconds(-1).ToString("yyyy-MM-dd 23:59:59");
                else
                    sDate = DateTime.Parse(lDay).AddSeconds(-1).ToString("yyyy-MM-dd 23:59:59");

                for (int j = 0; j < dtLelMonth.Rows.Count; j++)
                {
                    string gsjb = dtLelMonth.Rows[j]["公式级别"].ToString();

                    #region 平均
                    sql = "select * from T_sheet_sheetPara where 报表名称='" + repName + "' and 机组=0 and 参数类型='平均' and 公式级别=" + gsjb;

                    DataTable dtAvg = null;

                    if (rlDBType == "SQL")
                        dtAvg = DBsql.RunDataTable(sql, out errMsg);
                    else
                        dtAvg = DBdb2.RunDataTable(sql, out errMsg);

                    if (dtAvg != null && dtAvg.Rows.Count > 0)
                    {
                        for (int i = 0; i < dtAvg.Rows.Count; i++)
                        {
                            num = dtAvg.Rows[i]["小数点数"].ToString();

                            pName = dtAvg.Rows[i][mType].ToString();
                            //参数ID
                            ParaID = dtAvg.Rows[i]["参数ID"].ToString();
                            //参数顺序
                            id = int.Parse(dtAvg.Rows[i]["参数顺序"].ToString());

                            if (rtDBType == "EDNA")
                                ek.GetHisValueByTime(pName, sDate, ref ret, ref drvA);
                            else
                                pk.GetHisValue(pName, sDate, ref drvA);

                            if (id != 0)
                            {
                                zb[id] = dtAvg.Rows[i]["参数名"].ToString();
                                un[id] = dtAvg.Rows[i]["单位"].ToString();
                                lay[id] = dtAvg.Rows[i]["设计值"].ToString();

                                dv[id] += sh.ShowPoint(drvA.ToString(), num) + ",";
                                dvExcel[id] += drvA.ToString() + ",";
                            }

                            if (ParaID != "")
                            {
                                if (htUn.Contains(ParaID))
                                {
                                    str = htUn[ParaID] + drvA.ToString() + ",";
                                    htUn.Remove(ParaID);
                                    htUn.Add(ParaID, str);
                                }
                                else
                                {
                                    str = drvA.ToString() + ",";
                                    htUn.Add(ParaID, str);
                                }
                            }
                        }
                    }
                    #endregion

                    #region 累计
                    sql = "select * from T_sheet_sheetPara where 报表名称='" + repName + "' and 机组=0 and 参数类型='累计' and 公式级别=" + gsjb;

                    DataTable dtDiff = null;

                    if (rlDBType == "SQL")
                        dtDiff = DBsql.RunDataTable(sql, out errMsg);
                    else
                        dtDiff = DBdb2.RunDataTable(sql, out errMsg);

                    if (dtDiff != null && dtDiff.Rows.Count > 0)
                    {
                        for (int i = 0; i < dtDiff.Rows.Count; i++)
                        {
                            num = dtDiff.Rows[i]["小数点数"].ToString();

                            pName = dtDiff.Rows[i][mType].ToString();
                            //参数ID
                            ParaID = dtDiff.Rows[i]["参数ID"].ToString();
                            //参数顺序
                            id = int.Parse(dtDiff.Rows[i]["参数顺序"].ToString());

                            if (rtDBType == "EDNA")
                                ek.GetHisValueByTime(pName, sDate, ref ret, ref drvA);
                            else
                                pk.GetHisValue(pName, sDate, ref drvA);

                            if (id != 0)
                            {
                                zb[id] = dtDiff.Rows[i]["参数名"].ToString();
                                un[id] = dtDiff.Rows[i]["单位"].ToString();
                                lay[id] = dtDiff.Rows[i]["设计值"].ToString();

                                dv[id] += sh.ShowPoint(drvA.ToString(), num) + ",";
                                dvExcel[id] += drvA.ToString() + ",";
                            }

                            if (ParaID != "")
                            {
                                if (htUn.Contains(ParaID))
                                {
                                    str = htUn[ParaID] + drvA.ToString() + ",";
                                    htUn.Remove(ParaID);
                                    htUn.Add(ParaID, str);
                                }
                                else
                                {
                                    str = drvA.ToString() + ",";
                                    htUn.Add(ParaID, str);
                                }
                            }
                        }
                    }

                    #endregion

                    #region SQL

                    sql = "select * from T_sheet_sheetPara where 报表名称='" + repName + "' and 机组=0 and 参数类型='SQL' and 公式级别=" + gsjb;

                    DataTable dtSQL = null;

                    if (rlDBType == "SQL")
                        dtSQL = DBsql.RunDataTable(sql, out errMsg);
                    else
                        dtSQL = DBdb2.RunDataTable(sql, out errMsg);

                    if (dtSQL != null && dtSQL.Rows.Count > 0)
                    {
                        for (int i = 0; i < dtSQL.Rows.Count; i++)
                        {
                            num = dtSQL.Rows[i]["小数点数"].ToString();

                            sql = dtSQL.Rows[i]["SQL语句"].ToString();
                            //参数ID
                            ParaID = dtSQL.Rows[i]["参数ID"].ToString();
                            //参数顺序
                            id = int.Parse(dtSQL.Rows[i]["参数顺序"].ToString());

                            if (sql.Contains("&bt&") && sql.Contains("&et&"))
                            {
                                sql = sql.Replace("&bt&", fDay);
                                sql = sql.Replace("&et&", sDate);
                            }

                            if (rlDBType == "SQL")
                                obj = DBsql.RunSingle(sql, out errMsg);
                            else
                                obj = DBdb2.RunSingle(sql, out errMsg);

                            res = "";
                            if (obj != null && obj != "")
                            {
                                if (obj.ToString() != "")
                                    res = obj.ToString();
                                else
                                    res = "0";
                            }
                            else
                            { res = "0"; }


                            if (id != 0)
                            {
                                zb[id] = dtSQL.Rows[i]["参数名"].ToString();
                                un[id] = dtSQL.Rows[i]["单位"].ToString();
                                lay[id] = dtSQL.Rows[i]["设计值"].ToString();

                                dv[id] += sh.ShowPoint(res, num) + ",";
                                dvExcel[id] += sh.ShowPoint(res, "-1") + ",";
                            }

                            if (ParaID != "")
                            {
                                if (htUn.Contains(ParaID))
                                {
                                    str = htUn[ParaID] + res + ",";
                                    htUn.Remove(ParaID);
                                    htUn.Add(ParaID, str);
                                }
                                else
                                {
                                    str = res + ",";
                                    htUn.Add(ParaID, str);
                                }
                            }
                        }
                    }
                    #endregion

                    #region 电量
                    sql = "select * from T_sheet_sheetPara where 报表名称='" + repName + "' and 机组=0 and 参数类型='电量' and 公式级别=" + gsjb;

                    DataTable dtDL = null;

                    if (rlDBType == "SQL")
                        dtDL = DBsql.RunDataTable(sql, out errMsg);
                    else
                        dtDL = DBdb2.RunDataTable(sql, out errMsg);

                    if (dtDL.Rows.Count > 0 && dtDL != null)
                    {
                        for (int i = 0; i < dtDL.Rows.Count; i++)
                        {
                            num = dtDL.Rows[i]["小数点数"].ToString();

                            dlID = dtDL.Rows[i]["电量点"].ToString();
                            //参数ID
                            ParaID = dtDL.Rows[i]["参数ID"].ToString();
                            //参数顺序
                            id = int.Parse(dtDL.Rows[i]["参数顺序"].ToString());

                            drvA = double.Parse(pc.GetPower(drvA, dlID, fDay, lDay, bm).ToString());

                            mon = "";

                            if (dtDL.Rows[i]["公式"].ToString() != "")
                                mon = drvA.ToString() + dtDL.Rows[i]["公式"].ToString();
                            else
                                mon = drvA.ToString();

                            res = "";
                            try
                            { res = StrHelper.Cale(mon); }
                            catch
                            { res = "0"; }

                            if (id != 0)
                            {
                                zb[id] = dtDL.Rows[i]["参数名"].ToString();
                                un[id] = dtDL.Rows[i]["单位"].ToString();
                                lay[id] = dtDL.Rows[i]["设计值"].ToString();

                                dv[id] += sh.ShowPoint(res, num) + ",";
                                dvExcel[id] += sh.ShowPoint(res, "-1") + ",";
                            }

                            if (ParaID != "")
                            {
                                if (htUn.Contains(ParaID))
                                {
                                    str = htUn[ParaID] + res + ",";
                                    htUn.Remove(ParaID);
                                    htUn.Add(ParaID, str);
                                }
                                else
                                {
                                    str = res + ",";
                                    htUn.Add(ParaID, str);
                                }
                            }
                        }
                    }

                    #endregion

                    #region 其它
                    sql = "select * from T_sheet_sheetPara where 报表名称='" + repName + "' and 机组=0 and 参数类型='其它' and 公式级别=" + gsjb;

                    DataTable dtQT = null;

                    if (rlDBType == "SQL")
                        dtQT = DBsql.RunDataTable(sql, out errMsg);
                    else
                        dtQT = DBdb2.RunDataTable(sql, out errMsg);

                    if (dtQT.Rows.Count > 0 && dtQT != null)
                    {
                        for (int i = 0; i < dtQT.Rows.Count; i++)
                        {
                            num = dtQT.Rows[i]["小数点数"].ToString();
                            //参数ID
                            ParaID = dtQT.Rows[i]["参数ID"].ToString();
                            //参数顺序
                            id = int.Parse(dtQT.Rows[i]["参数顺序"].ToString());

                            #region 购网电量
                            if (dtQT.Rows[i]["参数名"].ToString() == "购网电量")
                            {
                                //string re = um.gwdl(fDay, lDay, 0);

                                //if (dtQT.Rows[i]["显示"].ToString() == "1")
                                //{
                                //    id = int.Parse(dtQT.Rows[i]["参数顺序"].ToString());

                                //    dv[id] += sh.ShowPoint(re.ToString(), num) + ",";

                                //    dvExcel[id] += sh.ShowPoint(re.ToString(), "-1") + ",";
                                //}

                                //if (dtQT.Rows[i]["参数ID"].ToString() != "")
                                //{
                                //    id = int.Parse(dtQT.Rows[i]["参数key"].ToString());

                                //    du[id] = dtQT.Rows[i]["参数ID"].ToString() + "," + re.ToString() + ";";
                                //}
                            }
                            #endregion

                            #region 入炉煤低位发热量
                            if (dtQT.Rows[i]["参数名"].ToString() == "入炉煤低位发热量")
                            {
                                //string rlm = um.rlmdwfrl("月报", fDay, lDay, 1);

                                //if (dtQT.Rows[i]["显示"].ToString() == "1")
                                //{
                                //    id = int.Parse(dtQT.Rows[i]["参数顺序"].ToString());

                                //    dv[id] += sh.ShowPoint(rlm, num) + ",";

                                //    dvExcel[id] += sh.ShowPoint(rlm, "-1") + ",";
                                //}

                                //if (dtQT.Rows[i]["参数ID"].ToString() != "")
                                //{
                                //    id = int.Parse(dtQT.Rows[i]["参数key"].ToString());

                                //    //if (num == "")
                                //    du[id] = dtQT.Rows[i]["参数ID"].ToString() + "," + rlm + ";";
                                //    //else
                                //    //    du[id] = dtQT.Rows[i]["参数ID"].ToString() + "," + sh.ShowPoint(rlm, num) + ";";
                                //}
                            }
                            #endregion
                        }
                    }
                    #endregion

                    #region 公式
                    sql = "select * from T_sheet_sheetPara where 报表名称='" + repName + "' and 机组=0 and 参数类型='公式' and 公式级别=" + gsjb;

                    DataTable dtMon = null;

                    if (rlDBType == "SQL")
                        dtMon = DBsql.RunDataTable(sql, out errMsg);
                    else
                        dtMon = DBdb2.RunDataTable(sql, out errMsg);

                    if (dtMon != null && dtMon.Rows.Count > 0)
                    {
                        for (int i = 0; i < dtMon.Rows.Count; i++)
                        {
                            num = dtMon.Rows[i]["小数点数"].ToString();
                            //参数ID
                            ParaID = dtMon.Rows[i]["参数ID"].ToString();
                            //参数顺序
                            id = int.Parse(dtMon.Rows[i]["参数顺序"].ToString());

                            string pMon = dtMon.Rows[i]["公式"].ToString();

                            string paras = dtMon.Rows[i]["公式参数"].ToString();

                            string month = pm.retMonByMonthAndYear(htGY, htUn, paras, pMon);

                            if (month != "")
                            {
                                res = "";
                                try
                                { res = StrHelper.Cale(month); }
                                catch
                                { res = "0"; }

                                if (id != 0)
                                {
                                    zb[id] = dtMon.Rows[i]["参数名"].ToString();
                                    un[id] = dtMon.Rows[i]["单位"].ToString();
                                    lay[id] = dtMon.Rows[i]["设计值"].ToString();

                                    dv[id] += sh.ShowPoint(res, num) + ",";
                                    dvExcel[id] += sh.ShowPoint(res, "-1") + ",";
                                }

                                if (ParaID != "")
                                {
                                    if (htUn.Contains(ParaID))
                                    {
                                        str = htUn[ParaID] + res + ",";
                                        htUn.Remove(ParaID);
                                        htUn.Add(ParaID, str);
                                    }
                                    else
                                    {
                                        str = res + ",";
                                        htUn.Add(ParaID, str);
                                    }
                                }
                            }
                        }
                    }

                    #endregion
                }

                LogHelper.WriteLog(LogHelper.EnLogType.Run, DateTime.Now.ToString("yyyy-MM-dd H:mm:ss") + "---" + repName + "----" + repType + "----全厂结束!");
            }

            #endregion

            #region 调试模式
            if (TSMS == "yes")
            {
                ArrayList listGy = new ArrayList(htGY.Keys);
                ArrayList listUn = new ArrayList(htUn.Keys);

                listGy.Sort();
                listUn.Sort();

                foreach (string skey in listGy)
                { LogHelper.WriteLog(LogHelper.EnLogType.Run, DateTime.Now.ToString("yyyy-MM-dd H:mm:ss") + "---" + repName + "----" + repType + "----" + skey + "---" + htGY[skey]); }

                foreach (string skey in listUn)
                { LogHelper.WriteLog(LogHelper.EnLogType.Run, DateTime.Now.ToString("yyyy-MM-dd H:mm:ss") + "---" + repName + "----" + repType + "----" + skey + "---" + htUn[skey]); }
            }
            #endregion

            list.Add(zb);
            list.Add(un);
            list.Add(lay);
            list.Add(dv);
            list.Add(dvExcel);

            return list;
        }
    }
}
